package com.project.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;

import com.opensymphony.xwork2.ActionContext;
import com.project.entity.*;
import com.project.service.*;
public class CheckManageAction {
	private String start,limit;
	private String jsonString;
	private String numberQuery;
	private String nameQuery;
	private ProjectCheckRecord record;

	private CheckManage	CheckManageService;
	private	ProjectCheckRecord projectCheckRecord;
	
	/*********************** setter and  getter***************************************/
	
	public ProjectCheckRecord getRecord() {
		return record;
	}
	public String getNameQuery() {
		return nameQuery;
	}
	public void setNameQuery(String nameQuery) {
		this.nameQuery = nameQuery;
	}
	public void setRecord(ProjectCheckRecord record) {
		this.record = record;
	}
	public String getNumberQuery() {
		return numberQuery;
	}
	public void setNumberQuery(String numberQuery) {
		this.numberQuery = numberQuery;
	}
	public ProjectCheckRecord getProjectCheckRecord() {
		return projectCheckRecord;
	}
	public void setProjectCheckRecord(ProjectCheckRecord projectCheckRecord) {
		this.projectCheckRecord = projectCheckRecord;
	}
	public CheckManage getCheckManageService() {
		return CheckManageService;
	}
	public void setCheckManageService(CheckManage checkManageService) {
		CheckManageService = checkManageService;
	}
	public String getStart() {
		return start;
	}
	public void setStart(String start) {
		this.start = start;
	}
	public String getLimit() {
		return limit;
	}
	public void setLimit(String limit) {
		this.limit = limit;
	}
	public String getJsonString() {
		return jsonString;
	}
	public void setJsonString(String jsonString) {
		this.jsonString = jsonString;
	}
	
	void clearValue(){
		this.start = null;
		this.start = null;
		this.numberQuery = null;
	}
	
	/**
	 * 获取验收记录
	 * @return
	 */
	public String getAllCheck(){
		ActionContext context = ActionContext.getContext();	
		if(context.getSession().get("right")==null){//如果没有登录 则没有权限查看内容
			this.setJsonString("{success:false,totalCount : " + 0
					+ ", list:}");
			return "success";
		}
		
		Map<String,String> map = new HashMap<String,String>();
		if(context.getSession().get("right") == "manager"){ //如果是普通管理员只能查看分发到普通管理员的记录
			int id = (Integer)context.getSession().get("id");
			map.put("followupperson", String.valueOf(id));
		}
		map.put("start", start);
		map.put("limit", limit);
		if(this.numberQuery != null && !this.numberQuery.equals("")){
			//根据number查询
			map.put("numberQuery", numberQuery);
		}
		if(this.nameQuery != null && !this.nameQuery.equals("")){
			//根据number查询
			map.put("nameQuery", nameQuery);
		}
		
		
		List<ProjectCheckRecord> list  = this.CheckManageService.getAllRecord(map);
		int size;
		if(this.nameQuery != null &&!this.nameQuery.equals("")
				|| this.numberQuery != null && !this.numberQuery.equals("")){
			size = list.size();
		}else{
			size = this.CheckManageService.countCheckRecord();
		}
		JSONArray jsonObject = JSONArray.fromObject(list);
		this.setJsonString("{success:true,totalCount:" + size+ ",list:" + jsonObject.toString() + "}");
		this.clearValue();
		return "success";
	}
	
	public String addCheckRecord(){
		if ( this.projectCheckRecord.getProjectnumber()!= null 
				&& !this.projectCheckRecord.getProjectnumber().equals("")){
			int i;
			i = this.CheckManageService.addCheckRecord(projectCheckRecord);
			if ( i == -1){
				this.setJsonString("{failure:true,totalCount:" +-1+ ",list:}");
			} else 
				this.setJsonString("{success:true,totalCount:" +i+ ",list:}");
			
		}
		this.projectCheckRecord = null;
		return "success";
	}
	
	/**
	 * 修改验收记录
	 */
	public String updateCheckRecord(){
		if(record.getProjectcheckrecordid() != null 
				&& !record.getProjectcheckrecordid().equals("")){
			int i = this.CheckManageService.updateCheckRecord(record);
			if(i == -1){
				this.setJsonString("{success:false,totalCount:" +i+ ",list:}");
			}else{
				int j;
				j = this.CheckManageService.acceptProject(record.getProjectnumber());
				if(j==-1)
					i = -3;
				this.setJsonString("{success:true,totalCount:" +i+ ",list:}");
			}
		}else{
			this.setJsonString("{success:false,totalCount:" +-2+ ",list:}");
		}
			
		return "success";
	}
}
